import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import componentsRouter from './modules/components'
import chartsRouter from './modules/charts'
import tableRouter from './modules/table'
import nestedRouter from './modules/nested'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

export const constantRoutes = [
  {
    path: '',
    component: Layout,
    redirect: 'manage',
    children: [
      {
        path: 'manage',
        component: () => import('@/pages/manage/index'),
        name: 'manage',
        meta: { title: '管理中心', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/pages/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/pages/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/pages/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/pages/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/pages/error-page/401'),
    hidden: true
  }
]

/**
 * asyncRoutes: dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/m',
    component: Layout,
    redirect: 'noRedirect',
    name: '基本资料',
    meta: {
      title: '基本资料',
      icon: 'component',
      roles: ['admin', 'editor']
    },
    children: [
      {
        path: 'category',
        component: () => import('@/pages/client/category'),
        name: '产品类别',
        meta: { title: '产品类别', noCache: true }
      },
      {
        path: 'pallet',
        component: () => import('@/pages/client/pallet'),
        name: '托盘信息',
        meta: { title: '托盘信息', noCache: true }
      },
      {
        path: 'department',
        component: () => import('@/pages/client/department'),
        name: '车间信息',
        meta: { title: '车间信息', noCache: true }
      },
      {
        path: 'supplier',
        component: () => import('@/pages/client/supplier'),
        name: '供应信息',
        meta: { title: '供应信息', noCache: true }
      },
      {
        path: 'customer',
        component: () => import('@/pages/client/customer'),
        name: '客户信息',
        meta: { title: '客户信息', noCache: true }
      }
    ]
  },
  {
    path: '/w',
    component: Layout,
    redirect: 'noRedirect',
    name: '作业管理',
    meta: {
      title: '作业管理',
      icon: 'example',
      roles: ['admin', 'editor']
    },
    children: [
      {
        path: 'in-storage',
        component: () => import('@/pages/order/in-storage'),
        name: '入库作业',
        meta: { title: '入库作业', noCache: true }
      },
      {
        path: 'out-storage',
        component: () => import('@/pages/order/out-storage'),
        name: '出库作业',
        meta: { title: '出库作业', noCache: true }
      },
      {
        path: 'pallet-out',
        component: () => import('@/pages/order/pallet-out'),
        name: '出空托盘',
        meta: { title: '出空托盘', noCache: true }
      },
      {
        path: 'check',
        component: () => import('@/pages/order/check'),
        name: '查看作业',
        meta: { title: '查看作业', noCache: true }
      },
      {
        path: 'move',
        component: () => import('@/pages/order/move'),
        name: '移库作业',
        meta: { title: '移库作业', noCache: true }
      }
    ]
  },
  {
    path: '/s',
    component: Layout,
    redirect: 'noRedirect',
    name: '仓库信息',
    meta: {
      title: '仓库信息',
      icon: 'table',
      roles: ['admin', 'editor']
    },
    children: [
      {
        path: 'stock',
        component: () => import('@/pages/storage/stock'),
        name: '库存信息',
        meta: { title: '库存信息', noCache: true }
      },
      {
        path: 'location',
        component: () => import('@/pages/storage/location'),
        name: '库位图示',
        meta: { title: '库位图示', noCache: true }
      },
      {
        path: 'quality',
        component: () => import('@/pages/storage/quality'),
        name: '产品状态',
        meta: { title: '产品状态', noCache: true }
      },
      {
        path: 'device',
        component: () => import('@/pages/device/index'),
        name: '设备状态',
        meta: { title: '设备状态', noCache: true }
      }
    ]
  },
  {
    path: '/permission',
    component: Layout,
    redirect: '/permission/page',
    alwaysShow: true, // will always show the root menu
    name: 'Permission',
    meta: {
      title: 'permission',
      icon: 'lock',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'page',
        component: () => import('@/pages/permission/page'),
        name: 'PagePermission',
        meta: {
          title: 'pagePermission',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'directive',
        component: () => import('@/pages/permission/directive'),
        name: 'DirectivePermission',
        meta: {
          title: 'directivePermission'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'role',
        component: () => import('@/pages/permission/role'),
        name: 'RolePermission',
        meta: {
          title: 'rolePermission',
          roles: ['admin']
        }
      }
    ]
  },

  /** when your routing map is too long, you can split it into small modules **/
  nestedRouter,
  chartsRouter,
  tableRouter,

  {
    path: '/excel',
    component: Layout,
    redirect: '/excel/export-excel',
    name: 'Excel',
    meta: {
      title: 'excel',
      icon: 'excel'
    },
    children: [
      {
        path: 'export-excel',
        component: () => import('@/views/excel/export-excel'),
        name: 'ExportExcel',
        meta: { title: 'exportExcel' }
      },
      {
        path: 'export-selected-excel',
        component: () => import('@/views/excel/select-excel'),
        name: 'SelectExcel',
        meta: { title: 'selectExcel' }
      },
      {
        path: 'export-merge-header',
        component: () => import('@/views/excel/merge-header'),
        name: 'MergeHeader',
        meta: { title: 'mergeHeader' }
      },
      {
        path: 'upload-excel',
        component: () => import('@/views/excel/upload-excel'),
        name: 'UploadExcel',
        meta: { title: 'uploadExcel' }
      }
    ]
  },

  {
    path: '/zip',
    component: Layout,
    redirect: '/zip/download',
    alwaysShow: true,
    name: 'Zip',
    meta: { title: 'zip', icon: 'zip' },
    children: [
      {
        path: 'download',
        component: () => import('@/views/zip/index'),
        name: 'ExportZip',
        meta: { title: 'exportZip' }
      }
    ]
  },

  componentsRouter,

  {
    path: '/icon',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/icons/index'),
        name: 'Icons',
        meta: { title: 'icons', icon: 'icon', noCache: true }
      }
    ]
  },

  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
